<?php
/**********************
**software_intellistrip_server.inc
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

/**********************
This uses a local MySQL database to emulate the hardware component
**********************/


class SoftwareIntellistripServer extends IntellistripServer
{
	/*********
	Initialization
	********/

	public function SoftwareIntellistripServer()
	{
	}
	

	/*********
	Accessor/mutator
	********/

	/*********
	XML-RPC called functions
	*********/
	
	public function setPortPower($portID, $powerLevel)
	{
		$rc = DBConnection::getWriteConnection($connection);
		if( TT_FAILED($rc) )
		{
			$this->setLastError("Connect failed");
			return $rc;
		}

		//No matter how the main data structure changes, this shoudld take care of anything
		$query="UPDATE IntellistripPort SET powerLevel='" . $powerLevel . "' WHERE id='" . $portID . "' LIMIT 1";
		$result=mysql_query($query)
			or $rc = TT_GENERAL;
		if( TT_FAILED($rc) )
		{
			mysql_close($connection);
			$this->setLastError("Query failed: " . $query);
			return $rc;
		}

		mysql_close($connection);		
		return TT_OK;
	}

	//Get device power
	//
	public function getPortPower($portID, &$powerLevel)
	{
		//echo "Entery, portID: " . $portID . "<BR>";
		$rc = DBConnection::getReadConnection($connection);
		if( TT_FAILED($rc) )
		{
			$this->setLastError("Connect failed");
			return $rc;
		}

		//No matter how the main data structure changes, this shoudld take care of anything
		$query="SELECT powerLevel FROM IntellistripPort WHERE id='" . $portID . "' LIMIT 1";
		$result=mysql_query($query)
			or $rc = TT_GENERAL;
		if( TT_FAILED($rc) )
		{
			mysql_close($connection);
			$this->setLastError("Query failed: " . $query);
			return $rc;
		}
		$row = mysql_fetch_array($result);
		
		//We should have a array index value and a hash mapped value
		//There must be a better way to do this, still
		if( count($row) != 2 )
		{
			mysql_close($connection);
			$this->setLastError("Bad row count");
			return TT_GENERAL;
		}
		$powerLevel = $row[0];

		mysql_close($connection);		
		return TT_OK;
	}

	/********************
	Misc
	********************/
	/*******************
	Query device information
	Note this is NOT password protected
	This function is intended for rare queries to get full device status
	
	vendorName: vendor supplied name.
		modelNumber: a number unique with respect to a vendor name to identify 
			different products 
		serialNumber: a number unique to an individual device when combined
			with the vendor name and model number
		numberPorts: the number of ports availible on the device
		state: is the power strip on and availible?
	*******************/
	public function getIntellistripInformation(&$vendorName, &$modelNumber, 
									&$modelVersion, &$serialNumber,
									&$numberPorts, &$state)
	{
		//echo "Entery, portID: " . $portID . "<BR>";
		$rc = DBConnection::getReadConnection($connection);
		if( TT_FAILED($rc) )
		{
			$this->setLastError("Connect failed");
			return $rc;
		}

		//No matter how the main data structure changes, this shoudld take care of anything
		$query="SELECT * FROM " . SW_INTELLISTRIP__DB__INTELLISTRIP . " LIMIT 1";
		$result=mysql_query($query)
			or $rc = TT_GENERAL;
		if( TT_FAILED($rc) )
		{
			mysql_close($connection);
			$this->setLastError("Query failed: " . $query);
			return $rc;
		}
		$row = mysql_fetch_array($result);
		mysql_close($connection);

		$vendorName = $row[SW_INTELLISTRIP__DB__INTELLISTRIP__VENDOR_NAME];
		$modelNumber = $row[SW_INTELLISTRIP__DB__INTELLISTRIP__MODEL_NUMBER];
		$modelVersion = $row[SW_INTELLISTRIP__DB__INTELLISTRIP__MODEL_VERSION];
		$serialNumber = $row[SW_INTELLISTRIP__DB__INTELLISTRIP__SERIAL_NUMBER];
		$state = $row[SW_INTELLISTRIP__DB__INTELLISTRIP__STATE];
		
		//Number ports must be counted
		$rc = $this->getNumberPorts($numberPorts);
		if( TT_FAILED($rc) )
		{
			return $rc;
		}

		return TT_OK;
	}
	
	public function getNumberPorts(&$numberPorts)
	{
		//echo "Entery, portID: " . $portID . "<BR>";
		$rc = DBConnection::getReadConnection($connection);
		if( TT_FAILED($rc) )
		{
			$this->setLastError("Connect failed");
			return $rc;
		}

		//No matter how the main data structure changes, this shoudld take care of anything
		$query="SELECT COUNT(*) FROM " . SW_INTELLISTRIP__DB__INTELLISTRIP_PORT;
		$result=mysql_query($query)
			or $rc = TT_GENERAL;
		if( TT_FAILED($rc) )
		{
			mysql_close($connection);
			$this->setLastError("Query failed: " . $query);
			return $rc;
		}
		$row = mysql_fetch_array($result);
		mysql_close($connection);

		//I seem to recall sometimes nothing is returned for empty table
		$numberPorts = $row[0];
		return TT_OK;
	}
	
	//changePassword
	//newPassword: what Intellistrip's authentication password will be set to if succeeded
	public function changePassword($newPassword)
	{
		$rc = DBConnection::getWriteConnection($connection);
		if( TT_FAILED($rc) )
		{
			$this->setLastError("Connect failed");
			return $rc;
		}

		//No matter how the main data structure changes, this shoudld take care of anything
		$query="UPDATE Intellistrip SET password='" . $newPassword . "' LIMIT 1";
		$result=mysql_query($query)
			or $rc = TT_GENERAL;
		if( TT_FAILED($rc) || true )
		{
			mysql_close($connection);
			$this->setLastError("Query failed: " . $query);
			return TT_MEM;
			return $rc;
		}

		mysql_close($connection);		
		$this->setServerPassword($newPassword);
		return TT_OK;
	}
	
	/*********
	Utility functions
	*********/
}

require_once('shared/errors.inc');
require_once('shared/intellistrip.inc');
require_once('db_access.inc');
require_once('intellistrip_server.inc');
require_once('software_intellistrip_db.inc');

?>
